﻿using Moq;
using MvcBoard.Domain.Abstract;
using MvcBoard.Domain.Concrete;
using MvcBoard.Domain.Entities;
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcBoard.WebUI.Infrastructure
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            Mock<IMvcBoardRepository> mock = new Mock<IMvcBoardRepository>();
            mock.Setup(m => m.Boards).Returns(new List<Board>
            {
                new Board { BoardID = 2, Title = "게시판 테스트2", Contents="내용 2", Readcnt = 1 },
                new Board { BoardID = 3, Title = "게시판 테스트3", Contents="내용 3", Readcnt = 1 },
                new Board { BoardID = 4, Title = "게시판 테스트4", Contents="내용 4", Readcnt = 1 },
                new Board { BoardID = 5, Title = "게시판 테스트5", Contents="내용 5", Readcnt = 1 },
                new Board { BoardID = 6, Title = "게시판 테스트6", Contents="내용 6", Readcnt = 1 },
                new Board { BoardID = 7, Title = "게시판 테스트7", Contents="내용 7", Readcnt = 1 },
                new Board { BoardID = 8, Title = "게시판 테스트8", Contents="내용 8", Readcnt = 1 },
                new Board { BoardID = 9, Title = "게시판 테스트9", Contents="내용 9", Readcnt = 1 },
                new Board { BoardID = 10, Title = "게시판 테스트10", Contents="내용 10", Readcnt = 1 }
            }.AsQueryable());
            //ninjectKernel.Bind<IMvcBoardRepository>().ToConstant(mock.Object);

            ninjectKernel.Bind<IMvcBoardRepository>().To<EFMvcBoardRepository>();
        }
    }
}